package org.jeecg.modules.travel.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.travel.code.CheckStatusCode;
import org.jeecg.modules.travel.dto.attraction.*;
import org.jeecg.modules.travel.entity.JhAttraction;
import org.jeecg.modules.travel.service.IJhAttractionService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.travel.vo.attraction.AppAttractionDetailVo;
import org.jeecg.modules.travel.vo.attraction.AppAttractionRecordVo;
import org.jeecg.modules.travel.vo.attraction.AttractionListVo;
import org.jeecg.modules.travel.vo.attraction.PcAttractionDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 景点表
 * @Author: jeecg-boot
 * @Date:   2025-04-19
 * @Version: V1.0
 */
@Tag(name="景点表")
@RestController
@RequestMapping("/travel/jhAttraction")
@Slf4j
public class JhAttractionController extends JeecgController<JhAttraction, IJhAttractionService> {
	@Autowired
	private IJhAttractionService jhAttractionService;
	
	/**
	 * 分页列表查询
	 *
	 * @param attractionDto
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "景点表-分页列表查询")
	@Operation(summary="景点表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AttractionListVo>> queryPageList(AttractionDto attractionDto,
														 HttpServletRequest req) {
		JhAttraction jhAttraction = new JhAttraction();
		BeanUtils.copyProperties(attractionDto, jhAttraction);
		// 自定义查询规则
        Map<String, QueryRuleEnum> customeRuleMap = new HashMap<>();
        // 自定义多选的查询规则为：LIKE_WITH_OR
        customeRuleMap.put("isFeatured", QueryRuleEnum.LIKE_WITH_OR);
        QueryWrapper<JhAttraction> queryWrapper = QueryGenerator.initQueryWrapper(jhAttraction, req.getParameterMap(),customeRuleMap);
		Page<JhAttraction> page = new Page<JhAttraction>(attractionDto.getPageNo(), attractionDto.getPageSize());
		IPage<AttractionListVo> pageList = jhAttractionService.queryPage(page, queryWrapper);
		return Result.OK(pageList);

	}
	
	/**
	 *   添加
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "景点表-添加")
	@Operation(summary="景点表-添加")
	@RequiresPermissions("org:jh_attraction:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AttractionAddDto dto) {
		String res = jhAttractionService.attractionAdd(dto);
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "景点表-编辑")
	@Operation(summary="景点表-编辑")
	@RequiresPermissions("org:jh_attraction:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AttractionEditDto dto) {
		String res = jhAttractionService.attractionEdit(dto, getLoginUser().getUsername());
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("编辑成功!");
	}

	 /**
	  *  审核
	  *
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "景点表-审核")
	 @Operation(summary="景点表-审核")
	 @RequiresPermissions("org:jh_attraction:auth")
	 @PostMapping(value = "/auth")
	 public Result<String> auth(@RequestBody @Valid AttractionAuthDto dto) {
		 String res = jhAttractionService.attractionAuth(dto,getUserId());
		 if (StringUtils.isNotBlank(res)) {
			 return Result.error(res);
		 }
		 return Result.OK("编辑成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "景点表-通过id删除")
	@Operation(summary="景点表-通过id删除")
	@RequiresPermissions("org:jh_attraction:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jhAttractionService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "景点表-批量删除")
	@Operation(summary="景点表-批量删除")
	@RequiresPermissions("org:jh_attraction:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jhAttractionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "景点表-通过id查询")
	@Operation(summary="景点表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<PcAttractionDetailVo> queryById(@RequestParam(name="id",required=true) String id) {
		return Result.OK(jhAttractionService.pcAttractionDetail(id));
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jhAttraction
    */
    @RequiresPermissions("org:jh_attraction:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JhAttraction jhAttraction) {
        return super.exportXls(request, jhAttraction, JhAttraction.class, "景点表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("org:jh_attraction:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JhAttraction.class);
    }

	/* ==================================== 移动端 ================================================= */

	 @Operation(summary="景点表-移动端分页列表查询")
	 @GetMapping(value = "/app/queryAttractionList")
	 public Result<IPage<AppAttractionRecordVo>> queryAttractionList(AppAttractionPageDto dto, HttpServletRequest req) {
		 JhAttraction attraction = new JhAttraction();
		 BeanUtils.copyProperties(dto,attraction);
		 QueryWrapper<JhAttraction> queryWrapper = QueryGenerator.initQueryWrapper(attraction, req.getParameterMap());
		 queryWrapper.lambda().eq(JhAttraction::getDelFlag, CommonConstant.DEL_FLAG_0)
				 .eq(JhAttraction::getCheckStatus, CheckStatusCode.APPROVED);
		 Page<JhAttraction> page = new Page<JhAttraction>(dto.getPageNo(), dto.getPageSize());
		 String userId = "";
		 if (isLogin()) {
			 userId = getUserId();
		 }
		 return Result.OK(jhAttractionService.queryAttractionList(dto,userId,page, queryWrapper));
	 }

	 @Operation(summary="景点表-移动端详情")
	 @GetMapping(value = "/app/appAttractionDetail")
	 public Result<AppAttractionDetailVo> appAttractionDetail(@RequestParam(name="id",required=true) String id) {
		 String userId = "";
		 if (isLogin()) {
			 userId = getUserId();
		 }
		 return Result.OK(jhAttractionService.appAttractionDetail(id,userId));
	 }

	 @Operation(summary="景点表-移动端随机列表查询")
	 @GetMapping(value = "/app/randomAttractionList")
	 public Result<IPage<AppAttractionRecordVo>> randomAttractionList(AppAttractionPageDto dto, HttpServletRequest req) {
		 String userId = "";
		 if (isLogin()) {
			 userId = getUserId();
		 }
		 return Result.OK(jhAttractionService.randomAttractionList(dto,userId));
	 }

}
